/**
 * @file Config.java
 *
 * @author Andrew Moss 
 * 
 * This file contains configuration data for the mote app.
 *
 */
package com.mossdev.android.moteapp;

import android.content.SharedPreferences;


/**
 * @author Andrew Moss
 *
 */
public class Config {
	
	//Constants
	//---------------------------------------------
	
	//To be used for log messages
	public static final String LogTag = "Mote";
	
	//Keys for shared prefs
	public static final String KEY_SERVER_PORT = "serverport";
	public static final String KEY_SERVER_ADDR = "serveraddr";
	
	public static final String KEY_CUSTOM_BUTTON_1 = "custom1";
	public static final String KEY_CUSTOM_BUTTON_2 = "custom2";
	public static final String KEY_CUSTOM_BUTTON_3 = "custom3";
	public static final String KEY_CUSTOM_BUTTON_4 = "custom4";
	public static final String KEY_CUSTOM_BUTTON_5 = "custom5";
	public static final String KEY_CUSTOM_BUTTON_6 = "custom6";
	
	//Default config vals
	private static final int DEFAULT_PORT = 21568;
	private static final String DEFAULT_ADDR = "192.168.1.64";
	
	public static final String DEFAULT_CUSTOM_BUTTON_1 = "custom1";
	public static final String DEFAULT_CUSTOM_BUTTON_2 = "custom2";
	public static final String DEFAULT_CUSTOM_BUTTON_3 = "custom3";
	public static final String DEFAULT_CUSTOM_BUTTON_4 = "custom4";
	public static final String DEFAULT_CUSTOM_BUTTON_5 = "custom5";
	public static final String DEFAULT_CUSTOM_BUTTON_6 = "custom6";
	
	
	//Variable Config Elements
	//---------------------------------------------
	
	//Persistent preferences
	private SharedPreferences prefs;
	private SharedPreferences.Editor prefs_editor;
	
	//Constructor
	//---------------------------------------------
	
	/**
	 * Constructor for Config Class.
	 * Will populate the Config object with the values in prefs.
	 * 
	 * @param prefs Preferences for MoteApp
	 */
	public Config(SharedPreferences prefs) {
		this.prefs = prefs;
		prefs_editor = this.prefs.edit();
	}

	//Getters and Setters
	//---------------------------------------------
	
	/**
	 * @param serverport the serverport to set
	 */
	public void setServerport(int serverport) {
		//Check range (could do better!)
		if (serverport < 1) return;
		this.prefs_editor.putInt(KEY_SERVER_PORT, serverport);
		this.prefs_editor.commit();
	}

	/**
	 * @return the serverport
	 */
	public int getServerport() {
		return this.prefs.getInt(KEY_SERVER_PORT, DEFAULT_PORT);
	}

	/**
	 * @param serveraddr the serveraddr to set
	 */
	public void setServeraddr(String serveraddr) {
		this.prefs_editor.putString(KEY_SERVER_ADDR, serveraddr);
		this.prefs_editor.commit();
	}

	/**
	 * @return the serveraddr
	 */
	public String getServeraddr() {
		return this.prefs.getString(KEY_SERVER_ADDR, DEFAULT_ADDR);
	}
	
	
	public String getCustomButton1() {
		return this.prefs.getString(KEY_CUSTOM_BUTTON_1, DEFAULT_CUSTOM_BUTTON_1);
	}
	public String getCustomButton2() {
		return this.prefs.getString(KEY_CUSTOM_BUTTON_2, DEFAULT_CUSTOM_BUTTON_2);
	}
	public String getCustomButton3() {
		return this.prefs.getString(KEY_CUSTOM_BUTTON_3, DEFAULT_CUSTOM_BUTTON_3);
	}
	public String getCustomButton4() {
		return this.prefs.getString(KEY_CUSTOM_BUTTON_4, DEFAULT_CUSTOM_BUTTON_4);
	}
	public String getCustomButton5() {
		return this.prefs.getString(KEY_CUSTOM_BUTTON_5, DEFAULT_CUSTOM_BUTTON_5);
	}
	public String getCustomButton6() {
		return this.prefs.getString(KEY_CUSTOM_BUTTON_6, DEFAULT_CUSTOM_BUTTON_6);
	}
	
	public void setCustomButton1(String custom_cmd) {
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_1, custom_cmd);
		this.prefs_editor.commit();
	}
	public void setCustomButton2(String custom_cmd) {
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_2, custom_cmd);
		this.prefs_editor.commit();
	}
	public void setCustomButton3(String custom_cmd) {
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_3, custom_cmd);
		this.prefs_editor.commit();
	}
	public void setCustomButton4(String custom_cmd) {
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_4, custom_cmd);
		this.prefs_editor.commit();
	}
	public void setCustomButton5(String custom_cmd) {
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_5, custom_cmd);
		this.prefs_editor.commit();
	}
	public void setCustomButton6(String custom_cmd) {
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_6, custom_cmd);
		this.prefs_editor.commit();
	}
	
	//--------------------------------
	
	/**
	 * Sets the default settings.
	 */
	public void setDefaults() {
		this.prefs_editor.putInt(KEY_SERVER_PORT, DEFAULT_PORT);
		this.prefs_editor.putString(KEY_SERVER_ADDR, DEFAULT_ADDR);
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_1, DEFAULT_CUSTOM_BUTTON_1);
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_2, DEFAULT_CUSTOM_BUTTON_2);
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_3, DEFAULT_CUSTOM_BUTTON_3);
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_4, DEFAULT_CUSTOM_BUTTON_4);
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_5, DEFAULT_CUSTOM_BUTTON_5);
		this.prefs_editor.putString(KEY_CUSTOM_BUTTON_6, DEFAULT_CUSTOM_BUTTON_6);
		this.prefs_editor.commit();
	}

}
